﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YnBaseClass2;
using YnPlcDataServer.Entities;
using YnPlcDataServer.Utils;

namespace YnPlcDataServer
{
    public partial class SvidEditForm : Form
    {
        #region 自定义变量
        public YnBaseClass2.Form.YnFormRight ynFormRight = new YnBaseClass2.Form.YnFormRight();
        public YnEditType ynEditType = YnEditType.Query;
        public bool bEditHasChanged = false;//bDataChanged = false, 
        public YnSvid ynSvid { get; set; }
        #endregion

        #region 窗体
        public SvidEditForm()
        {
            InitializeComponent();
        }

        private void SvidEditForm_Load(object sender, EventArgs e)
        {
            this.comboBox_dataType.SelectedIndexChanged -= new System.EventHandler(this.comboBox_dataType_SelectedIndexChanged);
            #region init
            var listDataType = Enum.GetValues(typeof(DataType))
                .Cast<Enum>()
                .Select(value => new
                {
                    (Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), typeof(DescriptionAttribute)) as DescriptionAttribute).Description,
                    value
                })
                .OrderBy(item => item.value)
                .ToList();
            comboBox_dataType.DataSource = listDataType;
            comboBox_dataType.DisplayMember = "Description";
            comboBox_dataType.ValueMember = "value";
            #endregion


            if (ynEditType == YnEditType.Edit || ynEditType == YnEditType.Query)
            {
                ShowData();
            }
            else
            {
                comboBox_dataType.SelectedValue = DataType.Coil;
                comboBox_dataType_SelectedIndexChanged(null, null);
                comboBox_dataType.Focus();
            }
            this.button_save.Enabled = false;
            bEditHasChanged = false;

            textBox_name.TextChanged += new System.EventHandler(EditChanged);
            numericUpDown_sortno.ValueChanged += new System.EventHandler(EditChanged);
            this.comboBox_dataType.SelectedIndexChanged += new System.EventHandler(this.comboBox_dataType_SelectedIndexChanged);

            textBox_address.TextChanged += new System.EventHandler(EditChanged);
            numericUpDown_length.ValueChanged += new System.EventHandler(EditChanged);
            numericUpDown_upper_spec_limit.ValueChanged += new System.EventHandler(EditChanged);
            numericUpDown_spec_target.ValueChanged += new System.EventHandler(EditChanged);
            numericUpDown_lower_spec_limit.ValueChanged += new System.EventHandler(EditChanged);
            this.comboBox_dataType.SelectedIndexChanged += new System.EventHandler(EditChanged);

        }
        private void SvidEditForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bEditHasChanged)
            {
                DialogResult dialogResult = MessageBox.Show("确认要放弃刚才的修改吗?", "确认...", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (dialogResult != DialogResult.Yes)
                {
                    e.Cancel = true;
                }
            }
        }
        //重写ProcessCmdKey的方法
        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            int WM_KEYDOWN = 256;
            int WM_SYSKEYDOWN = 260;
            if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
            {
                switch (keyData)
                {
                    case Keys.Escape:
                        this.Close();//esc关闭窗体
                        break;
                    case (Keys.Control | Keys.S):
                        button_save_Click(null, null);
                        break;
                }
            }
            return false;
        }
        #endregion

        #region 应用
        private void ShowData()
        {
            if (ynSvid != null)
            {
                textBox_name.Text = ynSvid.name;
                numericUpDown_sortno.Value = ynSvid.sortno;
                textBox_address.Text = ynSvid.address;
                DataType dataType = DataType.Coil;
                try
                {
                    dataType = (DataType)Enum.Parse(typeof(DataType), ynSvid.date_type);
                }
                catch (Exception) { }
                comboBox_dataType.SelectedValue = dataType;
                comboBox_dataType_SelectedIndexChanged(null, null); 
                numericUpDown_length.Value = ynSvid.length;

                numericUpDown_upper_spec_limit.Value = ynSvid.upper_spec_limit;
                numericUpDown_spec_target.Value = ynSvid.spec_target;
                numericUpDown_lower_spec_limit.Value = ynSvid.lower_spec_limit;
            }
            bEditHasChanged = false;
        }
        private void EditChanged(object sender, System.EventArgs e)
        {
            if (ynEditType == YnEditType.Edit || ynEditType == YnEditType.New)
            {
                bEditHasChanged = true;
                this.button_save.Enabled = true;
            }
        }
        private void comboBox_dataType_SelectedIndexChanged(object sender, EventArgs e)
        {
            numericUpDown_length.Value = 0;

            numericUpDown_length.Enabled = false;
            numericUpDown_upper_spec_limit.Enabled = true;
            numericUpDown_spec_target.Enabled = true;
            numericUpDown_lower_spec_limit.Enabled = true;
            if (comboBox_dataType.SelectedValue == null)
            {
                return;
            }
            DataType dataType = (DataType)comboBox_dataType.SelectedValue;
            if (dataType == DataType.String)
            {
                numericUpDown_length.Enabled = true;

                numericUpDown_upper_spec_limit.Enabled = false;
                numericUpDown_spec_target.Enabled = false;
                numericUpDown_lower_spec_limit.Enabled = false;

            }

        }
        private void button_save_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime dtServerTime = DateTime.Now;
                bool bCanSave = false;
                if (ynEditType == YnEditType.Edit)
                {
                    object object0 = SQLiteHelper.GetObject("select count(*) from YnSvid where name='" + textBox_name.Text.Trim() + "' and svid_id<>'" + ynSvid.svid_id + "'");
                    if (object0 == null)
                    {
                        MessageBox.Show("查询异常！", "警告...", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    int iCount = 0;
                    if (int.TryParse(object0.ToString(), out iCount))
                    {
                        if (iCount > 0)
                        {
                            MessageBox.Show("已经存在此状态变量【" + this.textBox_name.Text.Trim() + "】！", "警告...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }
                    DialogResult returnValue = MessageBox.Show("确认保存?", "确认保存...", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    if (returnValue != DialogResult.Yes)
                    {
                        return;
                    }
                    bCanSave = true;
                }
                else if (ynEditType == YnEditType.New)
                {
                    object object0 = SQLiteHelper.GetObject("select count(*) from YnSvid where name='" + textBox_name.Text.Trim() + "'");
                    if (object0 == null)
                    {
                        MessageBox.Show("查询异常！", "警告...", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    int iCount = 0;
                    if (int.TryParse(object0.ToString(), out iCount))
                    {
                        if (iCount > 0)
                        {
                            MessageBox.Show("已经存在此状态变量【" + this.textBox_name.Text.Trim() + "】！", "警告...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }
                    DialogResult returnValue = MessageBox.Show("确认增加?", "确认增加...", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                    if (returnValue != DialogResult.Yes)
                    {
                        return;
                    }
                    ynSvid = new YnSvid();
                    ynSvid.svid_id = SQLiteHelper.GetMaxID<YnSvid>("svid_id");
                    ynSvid.createTime = dtServerTime;
                    bCanSave = true;

                }

                if (bCanSave)
                {
                    ynSvid.sortno = (int)numericUpDown_sortno.Value;
                    ynSvid.updateTime = dtServerTime;
                    ynSvid.name = this.textBox_name.Text.Trim();
                    DataType dataType = (DataType)comboBox_dataType.SelectedValue;
                    ynSvid.date_type = dataType.ToString();
                    ynSvid.address = this.textBox_address.Text.Trim();
                    ynSvid.length = (int)numericUpDown_length.Value;
                    ynSvid.upper_spec_limit = numericUpDown_upper_spec_limit.Value;
                    ynSvid.spec_target = numericUpDown_spec_target.Value;
                    ynSvid.lower_spec_limit = numericUpDown_lower_spec_limit.Value;

                    if (ynEditType == YnEditType.Edit)
                    {
                        SQLiteHelper.Update(ynSvid, new { svid_id = ynSvid.svid_id });
                    }
                    else
                    {
                        SQLiteHelper.Insert(ynSvid);
                    }
                    this.bEditHasChanged = false;
                    this.DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败:" + ex.Message, "错误...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion





    }
}
